<?php include '../../../entries/restful.php';
/**
 *  录入电表信息
 *
 */
class Surface extends Restful{


    public function valid() {
        return [
            'post|metric_now|exist|metric_now必须传递！',
            'post|batch_now|exist|batch_now必须传递！',
            'post|metric_before|exist|metric_before必须传递！',
            'post|assets|exist|assets必须传递！',
            'post|assets_id|exist|assets_id必须传递！'
        ];
    }

    public function action(){

        $user = json_decode($this->session->getItem('user'),true);
        $user_id = $user['id'];
        $cmt_id = $user['cmt_id'];

        $batch_now = post('batch_now');
        $metric_before = post('metric_before');
        $assets = post('assets');
        $assets_id = post('assets_id');
        $build_id = post('build_id');
        $unit_id = post('unit_id');
        $metric_now = post('metric_now');

        $sql = 'select * from fee_order_item_temp where ' . $assets . '_id = ? and batch_id = ? ';
        $arg = [$assets_id,$batch_now];
        $nowInfo = Mdb::query(MDB_MAIN,$sql,$arg);
    
        if(empty($nowInfo)) {
            return Res::eLogic('请检查！有危险错误！');
        }

        $obj = [
            "assets" => $assets,
            "assets_id" => $assets_id,
            "metric_now" => $metric_now,
            "metric_before" => $mertric_before 
        ];
        $feeInfo = Fee::amount($nowInfo[0]['set_id'],$obj);

        $updateSql[] = 'update fee_order_item_temp set metric_now = ?,metric_before = ?,metric_unit = ?,amount = ? where id = ?';
        $updateArg[] = [$metric_now,$metric_before,$feeInfo['unit'],$feeInfo['amount'],$nowInfo[0]['id']];

        $updateSql[] = 'update fee_order_temp set metric_now = ?,metric_before = ?,metric_unit = ?,amount = amount + ? where id = ?';
        $updateArg[] = [$metric_now,$metric_before,$feeInfo['unit'],$feeInfo['amount'],$nowInfo[0]['order_id']];



        Mdb::write(MDB_MAIN,$updateSql,$updateArg);
        return Res::ok();
    }
}

new Surface();